var lock;

function queryString(ji) 
{
	hu = window.location.search.substring(1);
	gy = hu.split("&");
	
	for (i = 0; i<gy.length; i++) 
	{
		ft = gy[i].split("=");
		
		if (ft[0] == ji) 
		{
			return ft[1];
		}
	}
}

function lock_but()
{
	$(window).unload(function(){
		if(lock == true)
		{
			window.open("/intranet/logoff/", "_self")}
		}
	);

	var div = "<div class=\"bg_tr\" style=\"position: fixed; height: 100%; width: 100%; top: 0; background-color: black;\"></div>";
	
	$("body").append(div);
	$(".bg_tr").css('opacity',0).fadeTo("slow", "0.7");
	
	var div = "<div class=\"lock_scr\" style=\"padding: 10px; position: fixed; left: 50%; margin-left: -125px; margin-top: -85px; border:1px solid gray; top: 50%; height: 170px; width: 250px; background-color: white;\"></div>";
	
	$("body").append(div);
	$(".lock_scr").append($(".lock_div").html()).css('opacity',0).fadeTo("fast", "1");
	
	$("#senha").focus();
	
	lock = true;
}

function getObjectProperties(object) 
{
	var result = '';
	
	for (var property in object) 
	{
		result += property + ': ' + object[property] + '\r\n<br />';
	}
	
	return result;
}

function trim(str)
{
	return str.replace(/^\s+|\s+$/g,"");
}

function clone()
{
	var html = $(".cloneFilho").html();
	
	$(".clone").append("<br /><br />" + html).fadeIn();
	
	return false;
}

function marcaCheck(elm)
{
	var elm = "#c_" + elm;

	if($(elm).attr("checked"))
	{
		$(elm).attr("checked", "");
		$(elm).parent("td").parent("tr").css("background", "#FCFCFC").mouseover(function(){
			$(this).css("background", "#EFEFEF");
		}).mouseout(function(){
			$(this).css("background", "#FCFCFC");
		});
	}
	else
	{
		$(elm).attr("checked", "true");
		$(elm).parent("td").parent("tr").css("background", "#DADADA").mouseover(function(){
			$(this).css("background", "#DADADA");
		}).mouseout(function(){
			$(this).css("background", "#DADADA");
		});
	}
}

function paginacao(pagina)
{
	$("#paginas").val(pagina);
		
	$("#buscaClick").click();
}

function Message(msg)
{
	this.msg = msg;
	
	this.hide = function()
	{
		$("#message").animate({"top": "-20px", "opacity": 0}, 500);	
	}
	
	this.show = function()
	{
		$("#message").html(this.msg);
		//$("#message").fadeIn("fast");
		$("#message").show().css("opacity", 0).animate({"top": "0px", "opacity": 1}, 500);	
	}
	
	this.show();
}

function pesquisa(table, pagina)
{
	var msg    = new Message("Pesquisando...");	
	var pagina = !pagina?0:pagina;
	
	if($("#busca").val() == "Pesquisa")
	{
		$("#busca").css("color", "black");
		$("#busca").val("");
	}

	var values = $("#frmBusca").serialize();	
	var URI    = document.URL.split("//");
	
	URI = (URI[1] ? URI[1] : URI[0]).split("/");

	$.ajax({
		type    : "POST",
		url     : base + "/busca/index/table/" + table + "/control/" + URI[2] + "/?pagina=" + pagina,
		data    : values,
		success : function(result){
			$(".relatorioAjax").html(result);
			
			msg.hide();
		}
	});
}

function disabled()
{
	$(".disabled").focus(function(){
		$("input[type=submit]").focus();
	}).removeClass("required");
}

function permissaoForm()
{
	if($("#frmCad").length)
	{
		$.ajax({
			url     : base + "/AppBase/permissaoForm/",
			success : function(form){
				if(form)
				{
					form = form.split("|");
					
					$("input[type='text']").addClass("disabled");
					$("select").addClass("disabled");
				
					for(i in form)
					{
						$("#" + form[i]).removeClass("disabled");
					}
							
					disabled();	
				}			
			}
		});		
	}
}

function permissaoActions()
{
	if($("#BTN_actions").length)
	{
		$.ajax({
			url     : base + "/AppBase/permissaoActions/",
			success : function(action){
				if(action)
				{
					var i;
					
					action = action.split("|");
					
					$("img[alt='act']").hide();
				
					for(i in action)
					{
						$("#" + action[i]).show();
					}	
				}	
			}
		});		
	}
}

function menu()
{
	$("dd").hide();
	$("dt a").click(function(){
		var li = $(this).parent().next().find("li").html();
		
		if(li)
		{
			$("dd:visible").slideUp();
			$(this).parent().next().slideDown();
			
			return false;
		}		
	});
}